﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UpdateApp
{
    public partial class Default : System.Web.UI.Page
    {
        protected string display = "";
        protected string date = "";
        protected void Page_Load(object sender, EventArgs e)
        {                               
            if (!IsPostBack)
            {
                List<string> list = new List<string> { "WinCE", "WinXP" };

                list.ForEach(i =>
                {
                    if (Request.Browser.Platform.Equals(i, StringComparison.CurrentCultureIgnoreCase))
                        Response.Redirect("Simple.aspx");
                });

                string name = Request["key"];

                if (!string.IsNullOrEmpty(name))
                {
                    string version = ConfigurationManager.AppSettings[name];

                    string path = ConfigurationManager.AppSettings[name + "Path"];

                    if (string.IsNullOrEmpty(version) || string.IsNullOrEmpty(path))
                        return;

                    Response.ContentEncoding = Encoding.UTF8;

                    Response.Write(version + ">" + path);

                    Response.End();
                }

                if (!string.IsNullOrEmpty(Request["isSave"]))
                {
                    Common.ChangeConfig(Request["isSave"]);
                }

                if (!Common.CheckUserHost(HttpContext.Current))
                    display = "none";

                Session.Remove("FileList");

                //删除超出保存期限的文件夹
                Thread thread = new Thread(FileHelper.DelDirectories);
                thread.Start();
            }
            else
            {
                UploaderFileByBaidu up = new UploaderFileByBaidu();

                up.SaveFile();
            }

            date = DateTime.Now.ToString("yyyy-MM-dd");
        }

        protected void btnZip_ServerClick(object sender, EventArgs e)
        {
            List<string> fileList = Session["FileList"] != null ? (List<string>)Session["FileList"] : new List<string>();

            //文件数量为0时 将密码框的值更新到web.config的isSave中， 用于开启调试（isSave=1时 不验证IP白名单）
            if (fileList.Count == 0)
            {
                Common.ChangeConfig(this.password.Value.Trim());

                return;
            }

            bool result = FileHelper.CreateZipFile(fileList, this.password.Value);

            if (result)
                Session.Remove("FileList");
        }


    }
}